layui.define(['jquery'],
    function (exports) {
        var $ = layui.jquery;
        var kaPublic = new function () {
            this.strIsNull = function (value) { //字符串判断是空
                if (value !== null && value !== undefined && value !== '') {
                    return false
                } else {
                    return true;
                }
            }
            this.arrIsNull = function (value) { //数组判断是空
                if (value !== null && value !== undefined && value.length > 0) {
                    return false
                } else {
                    return true;
                }
            }
            this.strIsNumber = function (value) { //字符串判断是数字
                var regPos = /^\d+(\.\d+)?$/;
                var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/;
                if (regPos.test(value) && regNeg.test(value)) {
                    return true;
                } else {
                    return false;
                }
            }
            this.isAppPhoneOrExe = function () { //判断是否是手机端
                // console.log('是否在app中', jsBridge.inApp); jsBridge.view.openDevtools();
                var data = {
                    code: "",
                    type: "",
                    appMsg: ""
                };
                if (/Mobi|Android|iPhone/i.test(navigator.userAgent)) {
                    data.code = "200";
                    data.type = "APP";//手机端
                    data.appMsg = navigator.userAgent;
                } else {
                    data.code = "500";
                    data.type = "PC";//网页
                    data.appMsg = navigator.userAgent;
                }
                return data;
            }
            this.imgOpenView = function (that) { //图片放大查看
                var imgsrc = $(that).attr("src");
                var alt = $(that).attr("alt");
                layer.photos({
                    photos: {
                        "title": "图片查看",
                        "start": 0,
                        "data": [
                            {
                                "alt": alt,
                                "pid": 1,
                                "src": imgsrc,
                            }
                        ]
                    }
                });
            }
            this.uuidStr = function () {//获取UUID
                var s = [];
                var hexDigits = "0123456789abcdef";
                for (var i = 0; i < 36; i++) {
                    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
                }
                s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
                s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
                s[8] = s[13] = s[18] = s[23] = "-";
                var uuid = s.join("").replaceAll("-", "").replace(/\s+/g, '');;
                return uuid;
            }
            this.loadOptionHtml = function (dataSelect, idName, valueName) { //装载 Select
                var html = "";
                for (var i in dataSelect) {
                    html = html + `<option value="` + dataSelect[i][idName] + `">` + dataSelect[i][valueName] + `</option>`;
                }
                return html;
            }
            this.setUploginHtml = function (id, type) {
                if(type == 1) {
                    $("#" + id).html(`<a href="login.html">返回登录</a>`);
                } else if(type == 2) {
                    $("#" + id).html(`<a href="login.html" class="layui-btn layui-btn-primary layui-btn-sm">返回登录</a>`);
                }
            }
            this.initBodySetUp = function () {
                // 公共初始化设置 
                var publicTitle = "KaUltra Admin 4.0";
                $("title").html(publicTitle + " - " + $("title").html());// 设置 表头
                // 背景
                document.documentElement.style.setProperty("--global-primary-color", localStorage.getItem("theme-color-color"));
                //注册日志
                var kaPreviewTextValuesId = localStorage.getItem("kaPreviewTextValuesId");
                if (kaPreviewTextValuesId == undefined || kaPreviewTextValuesId == null || kaPreviewTextValuesId == "") {
                    var values = [];
                    values.push({ id: "kaLogId", time: new Date().getTime() });
                    localStorage.setItem("kaPreviewTextValuesId", JSON.stringify(values));
                } else {
                    var values = JSON.parse(kaPreviewTextValuesId);
                    var boolean = false;
                    for (var i in values) {
                        var value = values[i];
                        if (value.id == "kaLogId") {
                            boolean = true;
                            break;
                        }
                    }
                    if (!boolean) {
                        values.push({ id: "kaLogId", time: new Date().getTime() });
                    }
                    localStorage.setItem("kaPreviewTextValuesId", JSON.stringify(values));
                }
                // 獲取bodyUrl 
                var url = layui.url();
                var bodyUrl = url.search;
                bodyUrl.kaPublicTitle = publicTitle;
                return bodyUrl;
            }
            this.initClearData = function (){ // 清除
                layui.kaBgUrl.ajax();// 重新拉取 后端 URL

            }
            this.kaMySmallToolOpenView = function (id, title, url, area1, area2, maxmin) { // 应用弹窗
                layer.open({
                    type: 2,
                    title: title,
                    shadeClose: true,
                    shade: 0,
                    area: [area1, area2],
                    maxmin: maxmin == 1 ? true : false, // 允许全屏最小化
                    content: url // iframe 的 url
                });
            }
            this.kaMySmallToolOpenViewShizhong = function (id, title, url, area1, area2, maxmin) { // 时钟
                layer.open({
                    id: "kaMySmallToolOpenViewShizhong",
                    type: 1,
                    title: false,
                    area: [area1, area2],
                    shade: 0,
                    content: `
                        <div class="myshizhong">
                            <div class="box" id="clock">
                                <!-- 原点 -->
                                <div class="origin"></div>
                                <!-- 时钟数 -->
                                <div class="dot_box">
                                    <div class="dot">6</div>
                                    <div class="dot">5</div>
                                    <div class="dot">4</div>
                                    <div class="dot">3</div>
                                    <div class="dot">2</div>
                                    <div class="dot">1</div>
                                    <div class="dot">12</div>
                                    <div class="dot">11</div>
                                    <div class="dot">10</div>
                                    <div class="dot">9</div>
                                    <div class="dot">8</div>
                                    <div class="dot">7</div>
                                </div>
                                <!-- 时、分、秒针 -->
                                <div class="clock_line hour_line" id="hour_line"></div>
                                <div class="clock_line minute_line" id="minute_line"></div>
                                <div class="clock_line second_line" id="second_line"></div>
                                <!-- 日期 -->
                                <div class="date_info" id="date_info"></div>
                                <!-- 时间 -->
                                <div class="time_info" >
                                    <div class="time" id="hour_time"></div>
                                    <div class="time" id="minute_time"></div>
                                    <div class="time" id="second_time"></div>
                                </div>
                            </div>
                        </div>
                    `,
                    move: '.myshizhong',
                    success: function (layero, index, that) {
                        var $ = layui.$;
                        $("#kaMySmallToolOpenViewShizhong").parent().css("background-color", "#ffffff00");
                        $("#kaMySmallToolOpenViewShizhong").parent().css("box-shadow", "unset");
                        var clock = document.getElementById("clock");
                        function initNumXY() {
                            // 1、12个数字的位置设置
                            var radius = 160;//大圆半价
                            var dot_num = 360 / $(".dot").length;//每个div对应的弧度数
                            //每一个dot对应的弧度;
                            var ahd = dot_num * Math.PI / 180;
                            $(".dot").each(function (index, el) {
                                $(this).css({
                                    "left": 180 + Math.sin((ahd * index)) * radius,
                                    "top": 180 + Math.cos((ahd * index)) * radius
                                });
                            });
                            // 2、刻钟设置
                            for (var i = 0; i < 60; i++) {
                                clock.innerHTML += "<div class='clock-scale'> " +
                                    "<div class='scale-hidden'></div>" +
                                    "<div class='scale-show'></div>" +
                                    "</div>";
                            }
                            var scale = document.getElementsByClassName("clock-scale");
                            for (var i = 0; i < scale.length; i++) {
                                scale[i].style.transform = "rotate(" + (i * 6 - 90) + "deg)";
                            }
                        }
                        initNumXY();//调用上面的函数
                        //获取时钟id
                        var hour_line = document.getElementById("hour_line"),
                            minute_line = document.getElementById("minute_line"),
                            second_line = document.getElementById("second_line"),
                            date_info = document.getElementById("date_info"),//获取date_info
                            hour_time = document.getElementById("hour_time"),// 获去时间id
                            minute_time = document.getElementById("minute_time"),
                            second_time = document.getElementById("second_time");
                        //3、设置动态时间
                        function setTime() {
                            var nowdate = new Date();
                            //获取年月日时分秒
                            var year = nowdate.getFullYear(),
                                month = nowdate.getMonth() + 1,
                                day = nowdate.getDay(),
                                hours = nowdate.getHours(),
                                minutes = nowdate.getMinutes(),
                                seconds = nowdate.getSeconds(),
                                date = nowdate.getDate();
                            var weekday = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
                            // 获取日期id
                            date_info.innerHTML = year + "年" + month + "月" + day + "日   " + weekday[day];
                            hour_time.innerHTML = hours >= 10 ? hours : "0" + hours;
                            minute_time.innerHTML = minutes >= 10 ? minutes : "0" + minutes;
                            second_time.innerHTML = seconds >= 10 ? seconds : "0" + seconds;
                            console.log(year + "年" + month + "月" + day + "日   " + weekday[day]);
                            //时分秒针设置
                            var hour_rotate = (hours * 30 - 90) + (Math.floor(minutes / 12) * 6);
                            hour_line.style.transform = 'rotate(' + hour_rotate + 'deg)';
                            minute_line.style.transform = 'rotate(' + (minutes * 6 - 90) + 'deg)';
                            second_line.style.transform = 'rotate(' + (seconds * 6 - 90) + 'deg)';
                        }
                        // setTime();
                        setInterval(setTime, 1000);
                    }
                });
            }
            this.systemDateZhuanQDSj = function (dateStr) { // 系统时间 转 前端显示时间
                const date = new Date(dateStr);
                var result = layui.util.toDateString(date.getTime(), "yyyy-MM-dd HH:mm:ss");
                return result;
            }
            this.loadAccountLevel = function (params) {
                var html = `VIP`;
                return html + params;
            }
            this.loadLocationHref = function(id, type, startBoolean) {
                var currentUser = layui.kaCurrentUser.get().data;
                var mainSysApp = currentUser.mainSysApp;
                if (mainSysApp == undefined || mainSysApp == null || mainSysApp == "") {
                    var kaAjaxFormData = {"id": id};
                    layui.kaAjax.ajax(layui.kaBgUrl.url("setUserMainSysApp"), function (rs) {
                        if (rs.code == 200) {
                            top.currentUser = layui.kaCurrentUser.get().data;
                            if(type == 2){
                                top.location.href = "home.html";
                            }
                        }
                    }, kaAjaxFormData);
                } else {
                if (startBoolean) {
                        if (type == 2) {
                            top.location.href = "home.html";
                        }
                    } else {
                        var kaAjaxFormData = { "id": id };
                        layui.kaAjax.ajax(layui.kaBgUrl.url("setUserMainSysApp"), function (rs) {
                            if (rs.code == 200) {
                                top.currentUser = layui.kaCurrentUser.get().data;
                                if (type == 2) {
                                    top.location.href = "home.html";
                                }
                            }
                        }, kaAjaxFormData);
                    }
                }
            }
        };
        $(document).on('click', 'img', function () {
            top.layui.kaPublic.imgOpenView(this);
        });
        exports('kaPublic', kaPublic);
    }
)